package org.wukongcrm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.wukongcrm.domain.entity.Leads;
import org.wukongcrm.domain.model.LeadsVo;
import org.wukongcrm.domain.model.R;
import org.wukongcrm.service.LeadsService;

import java.util.Date;
import java.util.List;

/**
 * @author ZhongWang
 * @since 2025 03 14:10:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/leads")
public class LeadsController {
    private final LeadsService leadsService;

    @PostMapping
    public R<Leads> saveLeads (@RequestBody Leads leads) {
        log.debug("saveLeads: {}", leads);
        Integer ownerUserId = leads.getOwnerUserId();
        leads.setCreateUserId(ownerUserId);
        leads.setCreateTime(new Date());
        leadsService.save(leads);
        return R.success(leads);
    }

    @GetMapping
    public R<Page<LeadsVo>> queryAllLeads (@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size, String conditions, Integer leadsId) {
        Page<LeadsVo> page = new Page<>(current, size);
        List<LeadsVo> leadsList = leadsService.selectLeads(page, conditions, leadsId);
        page.setRecords(leadsList);
        return R.success(page);
    }

    @PutMapping
    public R<Leads> updateLeads (@RequestBody Leads leads) {
        log.debug("updateLeads: {}", leads);
        leads.setUpdateTime(new Date());
        leadsService.updateById(leads);
        return R.success(200, "success", leads);
    }

    @PutMapping("/owner")
    public R updateOwnerUser (Integer ownerUserId, Integer leadsNo) {
        return R.success(200, "success", leadsService.updateOwnerUser(ownerUserId, leadsNo));
    }

    @DeleteMapping("/{leadsId}")
    public R<Integer> deleteLeads (@PathVariable("leadsId") Integer leadsId) {
        log.debug("deleteLeads: {}", leadsId);
        leadsService.removeById(leadsId);
        return R.success(200, "success", leadsId);
    }
}
